package chapter7.eg3;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.MessageToByteEncoder;
import java.util.List;
import org.msgpack.MessagePack;
import org.msgpack.template.Templates;

public class MessagePackDecoder extends ByteToMessageDecoder {

  @Override
  protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
    byte[] bytes = new byte[in.readableBytes()];
    in.readBytes(bytes);
    MessagePack messagePack = new MessagePack();
    User user = messagePack.read(bytes, new User(), new UserTemplate());
    out.add(user);
  }
}
